//
//  GPBookCell.swift
//  BLH
//
//  Created by zhangshumeng on 2021/8/11.
//

import UIKit

class GPBookCell: UITableViewCell {

    private lazy var imgView: UIImageView = {
        let view = UIImageView()
        view.corner(radii: 6)
        return view
    }()
    
    private lazy var titleLabel: UILabel = {
        let view = UILabel()
        view.font = 15.font
        view.textColor = .black
        return view
    }()
    
    private lazy var updateLabel: UILabel = {
        let view = UILabel()
        view.text = "已更新: 24/24"
        view.font = 12.font
        view.textColor = .c999999
        return view
    }()
    
    private lazy var endLabel: UILabel = {
        let view = UILabel()
        view.text = "上次观看: 3eawaweqweqw"
        view.font = 12.font
        view.textColor = .c999999
        return view
    }()
    
    private lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.line
        return view
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        addSubviews()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func addSubviews() {
        contentView.addSubview(imgView)
        contentView.addSubview(titleLabel)
        contentView.addSubview(updateLabel)
        contentView.addSubview(endLabel)
        contentView.addSubview(lineView)
        
        imgView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(12)
            make.top.equalToSuperview().offset(12)
            make.height.equalTo(imgView.snp.width).dividedBy(0.8)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(imgView.snp.right).offset(8)
            make.top.equalTo(imgView.snp.top).offset(10)
        }
        
        updateLabel.snp.makeConstraints { make in
            make.left.equalTo(imgView.snp.right).offset(8)
            make.top.equalTo(titleLabel.snp.bottom).offset(12)
        }
        
        endLabel.snp.makeConstraints { make in
            make.left.equalTo(imgView.snp.right).offset(8)
            make.top.equalTo(updateLabel.snp.bottom).offset(8)
        }
        
        lineView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(12)
            make.right.equalToSuperview().offset(-12)
            make.bottom.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }

    func update(_ model: RecChildDataModel) {
        imgView.kf_set(model.image)
        titleLabel.text = model.title
        updateLabel.text = "已更新：\(model.count)/\(model.totalCount)"
        if let firstModel = model.items.filter({ $0.isSelected == true }).first {
            endLabel.text = "上次观看：\(firstModel.title)"
        } else {
            endLabel.text = ""
        }
    }
}
